package com.yegs.quartz.controller;


import com.yegs.quartz.entity.Msg;
import com.yegs.quartz.mysqlEntity.JobVo;
import com.yegs.quartz.service.impl.JobServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value="任务调度类",tags={"任务操作接口"})
@RestController("/job")
public class JobController {

    @Autowired
    private JobServiceImpl jobService;

    @GetMapping("/findAll")
    public Msg findAll(){
        List<JobVo> all = jobService.findAll();
        return Msg.success("jobs",all);
    }

    @ApiOperation("保存调度任务")
    @PostMapping("/save")
    public Msg saveJob(@RequestBody() JobVo job){
        try {
            jobService.save(job);
            return Msg.success("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.success(e.getMessage());
        }
    }

    @GetMapping("/triggerStatus")
    public Msg triggerStatus(@RequestParam(value = "id",required = true) Long id,
                             @RequestParam(value = "states",required = true) String states){
        try {
            jobService.triggerStatus(id,states);
            return Msg.success("更新成功!");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.success(e.getMessage());
        }
    }
    @DeleteMapping("/del")
    public Msg del(@RequestParam(value = "id",required = true) Long id){
        try {
            jobService.del(id);
            return Msg.success("删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.success(e.getMessage());
        }
    }
    @GetMapping("/trigger")
    public Msg trigger(@RequestParam(value = "id",required = true) Long id){
        try {
            jobService.trigger(id);
            return Msg.success("删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            return Msg.success(e.getMessage());
        }
    }



}
